﻿Imports System.Data.SqlClient
Public Class frmKhachHang
    Private strMaKH As String = ""
    Public booThanhCong As Boolean = False
    Sub New(Optional ByVal MaKH As String = "")

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        strMaKH = MaKH
    End Sub

    Private Sub frmKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If strMaKH <> "" Then
            DocDuLieu(strMaKH)
        End If
        If strMaKH <> "" Then
            Me.txtMaKH.ReadOnly = True
        End If
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        If validateInput() Then
            booThanhCong = CapNhat()
            If booThanhCong Then
                MsgBox("Lưu dữ liệu thành công!")
                strMaKH = Me.txtMaKH.Text
                Me.txtMaKH.ReadOnly = True
            Else
                MsgBox("Lưu dữ liệu thất bại!")
            End If
        End If

    End Sub

    Private Sub btnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub txtMaKH_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaKH.Enter
        txtMaKH.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtMaKH_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaKH.Leave
        Me.txtMaKH.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Sub txtTenKH_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenKH.Enter
        txtTenKH.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtTenKHLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenKH.Leave
        Me.txtTenKH.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Sub txtDiaChi_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDiaChi.Enter
        txtDiaChi.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtDiaChi_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDiaChi.Leave
        Me.txtDiaChi.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Function CapNhat() As Boolean
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Try
            myConn.Open()
            myComm.Connection = myConn
            If strMaKH <> "" Then
                strSql = "Update KhachHang set MaKH = @MaKH, TenKH = @TenKH, DiaChi = @DiaChi, SoDT = @SoDT, MaSoThue = @MaSoThue, TKNganHang = @TKNganHang where MaKH = @OriMaKH"
                myComm.Parameters.AddWithValue("@OriMaKH", strMaKH)
            Else
                strSql = "Insert into KhachHang(MaKH, TenKH, DiaChi, SoDT, MaSoThue, TKNganHang) values (@MaKH, @TenKH, @DiaChi, @SoDT, @MaSoThue, @TKNganHang)"
            End If
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@MaKH", txtMaKH.Text.Trim())
            myComm.Parameters.AddWithValue("@TenKH", txtTenKH.Text.Trim())
            myComm.Parameters.AddWithValue("@DiaChi", txtDiaChi.Text.Trim())
            myComm.Parameters.AddWithValue("@SoDT", getEmptyValue(txtSoDt.Text))
            myComm.Parameters.AddWithValue("@MaSoThue", getEmptyValue(txtMSThue.Text))
            myComm.Parameters.AddWithValue("@TKNganHang", getEmptyValue(txtTKNganHang.Text))

            myComm.ExecuteNonQuery()
        Catch ex As Exception
            Return False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return True
    End Function

    Private Sub DocDuLieu(ByVal MaKH As String)
        Dim strSql As String = "Select * from KhachHang where MaKH = @MaKH"
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            myAdapter.SelectCommand.Parameters.AddWithValue("@MaKH", MaKH)
            myAdapter.Fill(myTable)
            If myTable.Rows.Count > 0 Then
                Me.txtMaKH.Text = myTable.Rows(0).Item("MaKH")
                Me.txtTenKH.Text = myTable.Rows(0).Item("TenKH")
                Me.txtSoDt.Text = myTable.Rows(0).Item("SoDT")
                Me.txtMSThue.Text = myTable.Rows(0).Item("MaSoThue")
                Me.txtTKNganHang.Text = myTable.Rows(0).Item("TKNganHang")
                Me.txtDiaChi.Text = myTable.Rows(0).Item("DiaChi")
            Else
                Me.txtMaKH.Text = MaKH
                strMaKH = ""
            End If
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
    End Sub

    Private Function validateInput() As Boolean
        If Me.txtMaKH.Text.Trim = "" Then
            MsgBox(modError.KH_ERR_MAKH_REQUIRED, MsgBoxStyle.Information)
            txtMaKH.Focus()
            Return False
        Else
            If checkExist(Me.txtMaKH.Text.Trim, strMaKH) Then
                MsgBox(modError.KH_ERR_MAKH_EXISTED, MsgBoxStyle.Information)
                txtMaKH.Focus()
                Return False
            End If
        End If

        If Me.txtTenKH.Text.Trim = "" Then
            MsgBox(modError.KH_ERR_TENKH_REQUIRED, MsgBoxStyle.Information)
            txtTenKH.Focus()
            Return False
        End If

        If Me.txtDiaChi.Text.Trim = "" Then
            MsgBox(modError.KH_ERR_DIACHI_REQUIRED, MsgBoxStyle.Information)
            txtDiaChi.Focus()
            Return False
        End If

        Return True
    End Function

    Private Function checkExist(ByVal MaKH As String, Optional ByVal oriMaKH As String = "") As Boolean
        Dim strSql As String = "Select count(*) from KhachHang where MaKH = @MaKH "
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = False
        Try
            myConn.Open()
            If oriMaKH <> "" Then
                strSql &= " and MaKH <> @OrigMaKH"
                myComm.Parameters.AddWithValue("@OrigMaKH", oriMaKH)
            End If
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@MaKH", MaKH)
            myComm.Connection = myConn
            If myComm.ExecuteScalar() > 0 Then
                result = True
            End If
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Function getEmptyValue(ByVal value As String) As String
        If value.Trim = "" Then
            Return ""
        End If
        Return value.Trim
    End Function

    Private Function countData() As Integer
        Dim strSql As String = "Select count(*) from KhachHang"
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Integer
        Try
            myConn.Open()
            myComm.CommandText = strSql
            myComm.Connection = myConn
            result = myComm.ExecuteScalar()
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

End Class